# Maintainer: Václav Šmilauer <eu@doxos.eu>

_realname=aravis
pkgbase=mingw-w64-${_realname}
pkgname=(
	"${MINGW_PACKAGE_PREFIX}-${_realname}"
	"${MINGW_PACKAGE_PREFIX}-${_realname}-gst"
)
pkgver=0.8.22
pkgrel=3
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgdesc="A vision library for genicam based cameras"
url="https://wiki.gnome.org/Projects/Aravis"
makedepends=(
	 "rsync"
	 "${MINGW_PACKAGE_PREFIX}-meson"
	 "${MINGW_PACKAGE_PREFIX}-libnotify"
	 "${MINGW_PACKAGE_PREFIX}-pkgconf"
	 "${MINGW_PACKAGE_PREFIX}-libxml2"
	 "${MINGW_PACKAGE_PREFIX}-zlib"
	 "${MINGW_PACKAGE_PREFIX}-libusb"
	 "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
	 "${MINGW_PACKAGE_PREFIX}-gtk3"
	 "${MINGW_PACKAGE_PREFIX}-gstreamer"
	 "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
	 "${MINGW_PACKAGE_PREFIX}-cc"
)
options=('strip' 'staticlibs')
license=("LGPL2.1")
source=("https://github.com/AravisProject/aravis/releases/download/${pkgver}/${_realname}-${pkgver}.tar.xz")
sha256sums=('8cdad1e338f3faad12886dc71d61d882a15a58981a153831773da6713d1ff920')

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix=${MINGW_PREFIX} \
    --buildtype=plain \
    -Ddocumentation=disabled \
    -Dgst-plugin=enabled \
    -Dintrospection=enabled \
    -Dusb=enabled \
    -Dviewer=enabled \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson compile
}

check() {
  cd build-${MINGW_CHOST}

  # Add current build dirs to PATH to find DLLs
  PATH=$PWD/gst:$PWD/src:$PATH ${MINGW_PREFIX}/bin/meson test || true
}

_move_aravis-gst_to_dst(){
    # move everything from $1 which is not the base lib to $2
    rsync -armR -v \
        --remove-source-files \
        --include='bin/arv-viewer*' \
        --exclude='bin/*'  \
        --exclude='include/' \
        --exclude='pkgconfig/' \
        --exclude='libaravis*' \
        --exclude='licenses/' \
        --exclude='gir-*/' \
        --exclude='girepository*/' \
        "${1}/./" "${2}"
    # delete empty directories in $1
    find "${1}" -type d -empty -delete
}


package_aravis() {
	pkgdesc="A vision library for genicam based cameras (library, headers, tools)"
	depends=(
		"${MINGW_PACKAGE_PREFIX}-libnotify"
		"${MINGW_PACKAGE_PREFIX}-libxml2"
		"${MINGW_PACKAGE_PREFIX}-zlib"
		"${MINGW_PACKAGE_PREFIX}-libusb"
		"${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime"
	)
	cd build-${MINGW_CHOST}
	DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install
	_TMP=`realpath _tmp`
	mkdir ${_TMP}
	_move_aravis-gst_to_dst "${pkgdir}${MINGW_PREFIX}" "${_TMP}"
	rm -rf ${_TMP}
}

package_aravis-gst() {
	pkgdesc="A vision library for genicam based cameras (Gtk3 viewer, GStreamer plugin)"
	depends=(
		"${MINGW_PACKAGE_PREFIX}-aravis"
		"${MINGW_PACKAGE_PREFIX}-gtk3"
		"${MINGW_PACKAGE_PREFIX}-gst-plugins-good"
		"${MINGW_PACKAGE_PREFIX}-gstreamer"
	)
	# DESTDIR="$_TMP" ${MINGW_PREFIX}/bin/meson install
	cd build-${MINGW_CHOST}
	_TMP=`realpath _tmp`
	DESTDIR="${_TMP}" ${MINGW_PREFIX}/bin/meson install
	_move_aravis-gst_to_dst "${_TMP}" "${pkgdir}"
	rm -rf ${_TMP}
}


# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
